package com.applet.mallappletbackstage.service.impl;

import com.applet.mallappletbackstage.entity.Admin;
import com.applet.mallappletbackstage.entity.vo.AdminRegisterVO;
import com.applet.mallappletbackstage.mapper.AdminMapper;
import com.applet.mallappletbackstage.service.AdminService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author X
 * @since 2020-08-07
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    /***
     * 管理员登录
     * @param username
     * @param password
     * @return
     */
    @Override
    public Admin loginAdmin(String username, String password) {
        //条件构造
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.eq("username", username);
        //获取用户信息
        Admin admin = baseMapper.selectOne(adminQueryWrapper);
        //获取密码
        String adminPassword = admin.getPassword();
        boolean checkpw = BCrypt.checkpw(password, adminPassword);
        if (!checkpw) {
            return null;
        }
        return admin;
    }

    /***
     * 管理员注册
     * @param adminRegisterVO
     * @return
     */
    @Override
    public int registerAdmin(AdminRegisterVO adminRegisterVO) {
        Admin admin = new Admin();
        //加密密码
        adminRegisterVO.setPassword(BCrypt.hashpw(adminRegisterVO.getPassword(), BCrypt.gensalt()));
        //copy
        BeanUtils.copyProperties(adminRegisterVO, admin);
        return baseMapper.insert(admin);
    }

    /**
     * 获取用户列表
     * @param current
     * @return
     */
    @Override
    public Page<Admin> getUserList(Long current) {
        Page<Admin> adminPage = new Page<>(current, 15);
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        return baseMapper.selectPage(adminPage, adminQueryWrapper);
    }
}
